/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-09-13
 * Time: 13:23
 */
public class min_Window {
    public static String minWindow(String s, String t) {

        int[] t_arr=new int[128];
        for (int i = 0; i < t.length(); i++) {
            t_arr[t.charAt(i)]++;
        }
        int num=0;
        for (int i = 0; i < 128; i++) {
            if (t_arr[i]!=0){
                num++;
            }
        }
        int kinds=0;

        int[] ch_arr=new int[128];
        int minlen = Integer.MAX_VALUE, begin = -1;

        for (int right = 0,left=0; right <s.length() ; right++) {
            //进窗口
            char c=s.charAt(right);
            ch_arr[c]++;
            if (ch_arr[c]==t_arr[c]){
                kinds++;
            }
            while (kinds==num){

                if (right-left+1<minlen){
                    begin=left;
                    minlen=right-left+1;
                }


                char ch=s.charAt(left);

                if (ch_arr[ch]==t_arr[ch]){
                    kinds--;
                }
                ch_arr[ch]--;
                left++;
            }
        }
        if (begin==-1){
            return new String();
        }

        return s.substring(begin,begin+minlen);
    }

    public static void main(String[] args) {
        System.out.println(minWindow("ADOBECODEBANC", "ABC"));
    }
}
